﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KCMS.BusinessLogic.Interfaces;
using KCMS.BusinessLogic.Models;
using KCMS.DataAcsess.Interfaces;
using StructureMap;
using AutoMapper;

namespace KCMS.BusinessLogic.Engine
{
    public class ProductImageService: IProductImageService
    {
        private IProductImageManager pgm = ObjectFactory.GetInstance<IProductImageManager>();
        public Models.ProductImage getById(Guid id)
        {
            DataAcsess.Entitys.ProductImageEntity productImageEntity = pgm.getById(id);
            return Mapper.Map<DataAcsess.Entitys.ProductImageEntity, Models.ProductImage>(productImageEntity);
        }

        public IEnumerable<Models.ProductImage> getAll()
        {
            var result=(from t in pgm.getAll()
                       orderby t.ProductEntity.Article
                       select t).ToList();
            return Mapper.Map<IEnumerable<DataAcsess.Entitys.ProductImageEntity>, IEnumerable<Models.ProductImage>>(result);

        }

        public void update(ProductImage productImage)
        {
           
            productImage.DateOfChange = DateTime.Now;
            pgm.update(Mapper.Map<ProductImage, DataAcsess.Entitys.ProductImageEntity>(productImage));
        }


        public void save(ProductImage productImage)
        {
            productImage.DateOfCreate = DateTime.Now;
            productImage.DateOfChange = DateTime.Now;
            pgm.save(Mapper.Map<ProductImage, DataAcsess.Entitys.ProductImageEntity>(productImage));
        }
        public void delete(Guid id)
        {
            pgm.delete(id);
        }

        public IEnumerable<ProductImage> getByProductId(Guid productId)
        {
            var productImages=(from t in pgm.getAll()
                    where t.ProductId.Equals(productId)
                    select t).ToList();
            return Mapper.Map<IEnumerable<DataAcsess.Entitys.ProductImageEntity>,IEnumerable<Models.ProductImage>>(productImages);
        }
    }
}
